package com.hjx.movie.core.player.util;


import com.hjx.movie.core.player.entity.Folder;
import org.springframework.stereotype.Component;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Objects;


/**
 * <p>Title:</p>
 * <p>Description:</p>
 *
 * @author hjx
 * @date 2021/5/3 - 19:04
 */
@Component
public class PlayUtil {
    private static  Integer index = 0;
    private static final String ACG_PATH = "E:"+File.separator+"0InnerSisterRoom"+File.separator;
    private static final String AV_PATH = "E:"+File.separator+"0SisterRoom"+File.separator;


    public static ArrayList<Folder> initFileList(){
        ArrayList<Folder> folders = new ArrayList<>();
        getFolderList(new File(ACG_PATH),folders);
        getFolderList(new File(AV_PATH),folders);
        return folders;
    }

    public static Integer generatorId(){
        return ++index;
    }
    public static void getFolderList(File f,ArrayList<Folder> folders){
        if (f.isDirectory()){
            Folder dir = Folder.builder().id(generatorId()).hasChildren(true).name(f.getName()).type("dir").path(f.getPath()).children(new ArrayList<>()).build();
            folders.add(dir);
            for (File file: Objects.requireNonNull(f.listFiles())){
                getFolderList(file,dir.getChildren());
            }
        }else if(f.getName().matches("^.*\\.(mp4|flv|avi|rm|rmvb|wmv|mkv)$")) {
            String http = "";
            String path = f.getPath();
            String temp = path.replaceAll("\\\\","/");
            if (temp.contains("E:/0InnerSisterRoom")){
                http = temp.replace("E:/0InnerSisterRoom","http://192.168.0.104:8080/acg");
            }else {
                http= temp.replace("E:/0SisterRoom","http://192.168.0.104:8080/av");
            }
            try {
                folders.add(Folder.builder().id(generatorId()).hasChildren(false)
                        .name(f.getName()).size(f.length()/1024/1024+"MB").type("file")
                        .path(f.getPath())
                        .code(new String(Base64.getEncoder().encode(f.getPath().getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8)).build());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

//    public static void main(String[] args) {
//        String str = "E:\\0InnerSisterRoom\\0.0\\2020.02\\[桜都字幕组][200207][メリー・ジェーン]らぶりー第2話 無口な彼女";
//        final String s = new String(Base64.getEncoder().encode(str.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
//        System.out.println(s);
//        final String ss = new String(Base64.getDecoder().decode(s.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
//        System.out.println(ss);
//    }
}
